    //
//  StatDataViewController.m
//  WRY
//
//  Created by zhong sckj on 10-10-14.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "PopCantonVC_iPad.h"
#import "RootVC_iPad.h"
#import "WebPath.h"
#import "CommonJson.h"
#import "AppDelegate_iPad.h"
#import "StaticVariable.h"

@implementation PopCantonVC_iPad
@synthesize rootVC,dataArray,listTable_ipad;

//全局变量
extern NSString *namecode;
extern NSString *nameStr;
- (void)showConnectionError {
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
													message:@"Connection error" 
												   delegate:nil
										  cancelButtonTitle:@"OK" 
										  otherButtonTitles:nil];
	[alert show];
	[alert release];
}
- (void)requestForRegion:(NSString *)body methodName:(NSString *)methodName {
	[self showLoading:0 centerY:0];
	//返回按钮不让用
	NSString *bodyStr = [NSString stringWithFormat:@"LoginGuid=%@&%@",[[AppDelegate_iPad currentDelegate] GUID],body];
	[[CommonJson sharedInstance] requestToPath:[WebPath enterprisePath]
									methodName:methodName 
									bodyString:bodyStr 
									 targetObj:self
							  targetMethodName:@"eventForRequest:" 
									notifiName:@"RequestStaticsList"];
	
}
- (void)eventForRequest:(NSNotification *)notification {
	[self hideLoading];
	[[CommonJson sharedInstance] removeTarget:self notiName:[notification name]];
	NSObject *object  = [notification object];
	if ([object isKindOfClass:[NSDictionary class]]) {
		NSDictionary *dict = (NSDictionary *)object;
		if ([[dict objectForKey:@"Status"] intValue]) 
		{
			if ([[dict objectForKey:@"Data"] isKindOfClass:[NSArray class]])
			{
				[dataArray setArray:(NSArray *)[dict objectForKey:@"Data"]];
				[listTable_ipad reloadData];
			}
			else {
				//[self showConnectionError];
			}
		}
		else {
			//[self showConnectionError];
		}
		
	}
	else {
		//[self showConnectionError];
	}	
}

-(void)backViewController:(id)sender
{
	[rootVC.popController dismissPopoverAnimated:YES];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
   
	
	UIBarButtonItem *backItem=[[UIBarButtonItem alloc]initWithTitle:@"返回" 
															  style:UIBarButtonItemStyleBordered 
														target:self action:@selector(backViewController:)];
	self.navigationItem.leftBarButtonItem=backItem;
	[backItem release];
	dataArray = [[NSMutableArray alloc] init];
	//[self requestForRegion:@"CantonName=江苏省" methodName:@"Statistic__EnterpriseCantonDetail"];
	
	[super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	if (![dataArray count]) {
		[self requestForRegion:[NSString stringWithFormat:@"CantonName=%@&CantonLevel=%d",[[AppDelegate_iPad currentDelegate]showCantonName],([[[AppDelegate_iPad currentDelegate] userType]intValue]+1)] methodName:@"Statistic__EnterpriseCanton"];
	}
}
#pragma mark UITableViewDelegate & DataSource
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
	return [dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *Identifier_ipad = @"StaticsDetailCell_iPad";
	UITableViewCell *cell=[self.listTable_ipad dequeueReusableCellWithIdentifier:Identifier_ipad];
	if(cell == nil) {
		 cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
									reuseIdentifier:Identifier_ipad];
	}
	if ([dataArray count]>0) {
		NSDictionary *dict = [dataArray objectAtIndex:indexPath.row];
		cell.textLabel.text=(NSString *)[dict objectForKey:@"CantonName"];
	}
	else {
		cell.textLabel.text =@"没有记录";
	}
 	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSString *selectNamecode;
	NSString *selectName;
	NSDictionary *dict = [dataArray objectAtIndex:indexPath.row];
	selectNamecode=[dict objectForKey:@"CantonCodes"];
	selectName=[dict objectForKey:@"CantonName"];
	
	nameStr=selectName;
	//江苏省的传空
	namecode=[selectNamecode length]>20?@"":selectNamecode;

	
	[rootVC addEnterpriseVC:1000 enterType:@""];
	[rootVC.popController dismissPopoverAnimated:YES];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
	if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight)
	{
		return YES;
	}
    else 
	{
		return NO;
	}
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
	
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	
}


- (void)dealloc {
	[listTable_ipad release];
	[dataArray release];
	[rootVC release];
    [super dealloc];
}


@end
